package com.sakura.taos.tool.manipulator.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.NoArgsConstructor;

import static com.sakura.taos.tool.manipulator.conf.ConstantConf.DATA_BASE_GROUP_MAIN_NAME;
import static com.sakura.taos.tool.manipulator.conf.ConstantConf.DATA_BASE_GROUP_SEPARATOR;
import static com.sakura.taos.tool.manipulator.tool.Tool.stringIsNotNull;

/**
 * 数据库连接信息
 * @Author: 贺坤
 * @Date: 2021/1/26 20:27
 */
@Data
@NoArgsConstructor
@TableName(value="SYS_DBLINK")
public class SysDbLink {
    private static final String DEIVER_CLASS_NAME  = "com.taosdata.jdbc.TSDBDriver";

    /**
     * id序号\主键
     */
    @TableId(type=IdType.ASSIGN_ID)
    private Long id  ;
    /**
     * 连接地址
     */
    private String hostname;
    /**
     * 连接端口
     */
    private Integer port = 6030;
    /**
     * 数据库登录账号
     */
    private String username = "root";
    /**
     * 数据库密码
     */
    private String password = "taosdata";
    /**
     *   库名称
     */
    @TableField(exist = false)
    private String  dataBaseName ;
    /**
     * 连接状态
     */
    @TableField(exist = false)
    private Boolean  connect = false;

    public SysDbLink(String hostname) {
        this.hostname = hostname;
    }

    public MyDataSource getDataSource(){
        MyDataSource dataSourceDTO = new MyDataSource();
        String myDataBaseName = stringIsNotNull(getDataBaseName()) ? "" : "/" + getDataBaseName();
        dataSourceDTO.setPollName(computePollName());
        dataSourceDTO.setDriverClassName(DEIVER_CLASS_NAME);
//        jdbc:TAOS://192.168.20.225:6030/test
        dataSourceDTO.setUrl("jdbc:TAOS://"+getHostname()+":"+getPort()+ myDataBaseName);
        dataSourceDTO.setUsername(getUsername());
        dataSourceDTO.setPassword(getPassword());
        return dataSourceDTO;
    }

    /**
     * 计算判断获取数据源分组名称
     * @return
     */
    public String computePollName(){
        String myDataBaseName = stringIsNotNull(getDataBaseName()) ? DATA_BASE_GROUP_SEPARATOR+DATA_BASE_GROUP_MAIN_NAME : DATA_BASE_GROUP_SEPARATOR + getDataBaseName();
        return getHostname()+ myDataBaseName;
    }



}
